package com.study.spark.ml.movie.basic

import java.awt.Font

import com.study.spark.ml.movie.Util
import org.jfree.chart.axis.CategoryLabelPositions
import scalax.chart.module.ChartFactories

/**
  * 用户的职业分布柱状图
  *
  * @author: stephen.shen
  * @create: 2019-04-09 18:39
  */
object UserOccupationChart {
  def main(args: Array[String]) {
    val userDataFrame = Util.getUserDataDF()

    val occupation_groups = userDataFrame.groupBy("occupation").count()
    val occupation_groups_sorted = occupation_groups.sort("count")
    // 打印一下统计结果
    occupation_groups_sorted.show()
    val occupation_groups_collection = occupation_groups_sorted.collect()

    val ds = new org.jfree.data.category.DefaultCategoryDataset
    val mx = scala.collection.immutable.ListMap()
    // 数据封装成显示用的格式
    for (x <- 0 until occupation_groups_collection.length) {
      val occ = occupation_groups_collection(x)(0)
      val count = Integer.parseInt(occupation_groups_collection(x)(1).toString)
      ds.addValue(count, "User Occupations", occ.toString)
    }

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN, 5);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()

    Util.spark.sparkContext.stop()
  }
}
